local jinqu = fk.CreateSkill{
  name = "wk_heg__jinqu",
}

Fk:loadTranslationTable{
  ["wk_heg__jinqu"] = "进趋",
  [":wk_heg__jinqu"] = "每回合限一次，当你不因使用或打出而失去牌后，你可令一名与你势力相同的角色摸两张牌，然后其将手牌弃至X张（X为此回合进入弃牌堆的牌类型数）。",
  ["#wk_heg__jinqu-choose"] = "进趋：选择一名与你势力相同的角色，令其摸两张牌",

  ["$wk_heg__jinqu1"] = "建上昶水城，以逼夏口！",
  ["$wk_heg__jinqu2"] = "通川聚粮，伐吴之业，当步步为营。",
}

local H = require "packages/ol_hegemony/util"

jinqu:addEffect(fk.AfterCardsMove, {
  anim_type = "drwacard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(jinqu.name) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0  then
      for _, move in ipairs(data) do
        if move.from and move.from == player and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResponse then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, player) end)
    if #targets == 0 then return end
    local to = room:askToChoosePlayers(player, {
        targets = targets, 
        min_num = 1, 
        max_num = 1, 
        prompt = "#wk_heg__jinqu-choose", 
        skill_name = jinqu.name, 
        cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {to = to[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to
    if to then
      to:drawCards(2, self.name)
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      local end_id = turn_event.id
      local cards = {}
      room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              local card_type = Fk:getCardById(info.cardId, true).type
              if room:getCardArea(info.cardId) == Card.DiscardPile and not table.contains(cards, card_type) and card_type then
                table.insertIfNeed(cards, card_type)
              end
            end
          end
        end
        return false
      end, end_id)
      local discard_num = to:getHandcardNum() - #cards
      if discard_num > 0 then
        room:askToDiscard(to, {
            min_num = discard_num, 
            max_num = discard_num, 
            include_equip = false, 
            skill_name = jinqu.name, 
            cancelable = false,
            skip = false,
        })
      end
    end
  end,
})

return jinqu
